import { Request } from 'express';

/**
 * @description: 获取客户端真实 IP
 * @param {Request} req
 */
export function getRealIp(req: Request): string {
  const result = req.headers['x-forwarded-for'] || req.headers['x-real-ip'] || req.socket.remoteAddress || req.ip;

  if (!result || result === '::1' || result === '127.0.0.1') {
    return '0.0.0.0';
  }

  return Array.isArray(result) ? result[0] : result;
}
